package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.EditText;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.SearchUserAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.data.network.entity.AtUserEntity;
import com.zw.yzk.kabu.data.network.entity.SearchUserEntity;
import com.zw.yzk.kabu.internal.component.DaggerSearchUserComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.AtUserModule;
import com.zw.yzk.kabu.internal.module.FollowModule;
import com.zw.yzk.kabu.internal.module.SearchUserModule;
import com.zw.yzk.kabu.presenter.SearchUserPresenter;
import com.zw.yzk.kabu.utils.AppUtils;
import com.zw.yzk.kabu.view.view.SearchUserView;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.loadmore.DefaultLoadMoreViewCreator;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by zhanwei on 2017/2/5.
 */

public class SearchUserActivity extends BaseActivity implements SearchUserView, SearchUserAdapter.OnItemClickedListener {

    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.search)
    EditText search;
    @Bind(R.id.result)
    RecyclerView result;

    @Inject
    SearchUserPresenter presenter;
    private SearchUserAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search_user;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {

    }

    @Override
    public void initComponent() {
        DaggerSearchUserComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .searchUserModule(new SearchUserModule())
                .followModule(new FollowModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().getDecorView().postDelayed(new Runnable() {
            @Override
            public void run() {
                AppUtils.popupSoftKeyboard(search);
            }
        }, 100);

        initRecyclerView();
        getSearchResult(0, "");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        AppUtils.HideKeyboard(search);
    }


    @Override
    public void onItemClicked(SearchUserEntity.User user) {
        Intent intent = new Intent(this, HomePageActivity.class);
        intent.putExtra(ActivityConstant.HOME_PAGE_ID, user.id);
        startActivity(intent);
    }

    @Override
    public void onConcernedBtnClicked(Boolean concern, SearchUserEntity.User user) {
        presenter.follow(user.id, concern);
    }

    @Override
    public void searchUserSussess(List<SearchUserEntity.User> list) {
        if (refreshLayout.isLoading()) {
            adapter.add(list);
            refreshLayout.loadMoreFinish(list.size() > 0 ? State.LOADING_SUCCEED : State.LOAD_ALL);
        } else {
            adapter.refresh(list);
            refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
        }
    }

    @Override
    public void searchFailed() {
        refreshLayout.finishRefreshingOrLoadingStatus(false);
    }

    @OnClick(R.id.cancel)
    public void cancel() {
        finish();
    }

    @OnTextChanged(value = R.id.search, callback = OnTextChanged.Callback.TEXT_CHANGED)
    void onTextChanged(CharSequence s, int start, int before, int count) {
        getSearchResult(0, s.toString());
    }

    /**
     * 初始化recyclerview
     */
    private void initRecyclerView() {
        result.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new SearchUserAdapter(null);
        adapter.setOnItemClickedListener(this);
        result.setAdapter(adapter);

        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreViewCreator(new DefaultLoadMoreViewCreator())
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        getSearchResult(0, search.getText().toString());
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                        getSearchResult(adapter.getItemCount(), search.getText().toString());
                    }
                }).build();
    }

    /**
     * 获取搜索结果
     */
    private void getSearchResult(int start, String key) {
        presenter.searUser(key, start);
    }
}
